<template>
  <div class="Carousel">
    <!-- 添加 -->
    <el-row :gutter="20" class="Carousel-Info">
      <el-col :xs="6" :sm="4" :md="3" :lg="2" :xl="2" :offset="1">
        <el-button type="primary" class="Carousel-Info-li" size="small" @click="CarouselShow = true">添加</el-button>
      </el-col>
    </el-row>
    <!-- 查询列表 -->
    <el-row class="Carousel-list">
      <el-col :span="24">
        <el-table :data="tableData" border style="width: 100%">
          <el-table-column type="index" label="序" width="50">
          </el-table-column>
          <el-table-column prop="bannerName" label="名称" width="120">
          </el-table-column>
          <el-table-column label="图片" width="320">
            <template slot-scope="scope">
              <img style="width:200px;" :src="scope.row.bannerUrl" alt="">
            </template>
          </el-table-column>
          <el-table-column prop="isExternal" label="跳转类型" width="80">
          </el-table-column>
          <el-table-column prop="urlLink" label="外部链接" width="420">
          </el-table-column>
          <el-table-column prop="pageLink" label="内部链接" width="220">
          </el-table-column>
          <el-table-column prop="index" label="排序" width="120">
          </el-table-column>
          <el-table-column prop="address" label="操作" width="220">
            <template slot-scope="scope">
              <el-button @click="CarouseIsShowSubmit(scope.row.id,scope.row.isShow)">{{ scope.row.isShow?'隐藏':'显示' }}</el-button>
              <el-button @click="CarouselUpdateSubmit(scope.row.id)">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <!-- 添加或编辑关键词 -->
    <el-dialog title="添加轮播图" :visible.sync="CarouselShow">
      <el-form :model="CarouselForm" :rules="CarouselRules" ref="CarouselForm">
        <el-form-item label="名称" :label-width="CarouselWidth" prop="bannerName">
          <el-input v-model="CarouselForm.bannerName" autocomplete="off" placeholder="请输入关键词"></el-input>
        </el-form-item>
        <el-form-item label="图片" :label-width="CarouselWidth" prop="urlLink">
          <el-upload action="https://source.51shopro.com/api/v1/uploadToOss" :limit="1" list-type="picture-card" :on-preview="handlePictureCardPreview" :on-success="imguploadSuccess" :on-remove="handleRemove"
            :file-list="fileList">
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </el-form-item>
        <el-form-item label="跳转类型" :label-width="CarouselWidth" prop="isExternal">
          <el-select v-model="CarouselForm.isExternal" placeholder="请选择跳转类型">
            <el-option label="外部连接" :value="1"></el-option>
            <el-option label="内部连接" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="外部链接地址" :label-width="CarouselWidth" prop="urlLink">
          <el-input v-model="CarouselForm.urlLink" autocomplete="off" placeholder="请输入外部链接地址"></el-input>
        </el-form-item>
        <el-form-item label="内部链接地址" :label-width="CarouselWidth" prop="pageLink">
          <el-input v-model="CarouselForm.pageLink" autocomplete="off" placeholder="请输入内部链接地址"></el-input>
        </el-form-item>
        <el-form-item label="是否显示" :label-width="CarouselWidth" prop="isShow">
          <el-input v-model="CarouselForm.isShow" autocomplete="off" placeholder="请输入是否显示"></el-input>
        </el-form-item>
        <el-form-item label="排序" :label-width="CarouselWidth" prop="index">
          <el-input v-model="CarouselForm.index" autocomplete="off" placeholder="请输入排序"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="CarouselSubmit('CarouselForm')" type="primary">添 加</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { http } from "../../http";

export default {
  data() {
    var validatelinl = (rule, value, callback) => {
      console.log(rule, value)
      let linlRG = /\bhttps?:\/\/\S+?\.(?:png|jpe?g|gif|bmp)\b/g;
      if (value === '') {
        callback(new Error('请输入内容'));
      } else if (!linlRG.test(value)) {
        callback(new Error('请输入正确的网址'));
      } else {
        callback();
      }
    };
    return {
      fileList: [],
      tableData: [],
      // 上传
      dialogImageUrl: '',
      dialogVisible: false,
      // 弹出层
      CarouselShow: false,
      CarouselForm: {
        bannerName: '',
        isExternal: '',
        urlLink: '',
        pageLink: '',
        bannerUrl: '',
        isShow: '',
        index: 0
      },
      CarouselRules: {
        bannerName: [
          { required: true, message: '请输入轮播图名称', trigger: 'blur' },
          { min: 1, max: 100, message: '长度为 1~100 个字符', trigger: 'blur' }
        ],
        isExternal: [
          { required: true, message: '请选择跳转类型', trigger: 'change' }
        ],
        urlLink: [
          { required: true, message: '请输入内容', trigger: 'change' }
        ],
        pageLink: [
          { required: true, message: '请输入内容', trigger: 'change' }
        ],
        index: [
          { required: true, message: '请输入内容', trigger: 'change' }
        ],
        isShow: [
          { required: true, message: '请输入内容', trigger: 'change' }
        ],
        bannerUrl: [
          { required: true, message: '请输入内容', trigger: 'change' }
        ],
      },
      CarouselWidth: '120px'
    }
  },
  created() {
    this.onGetBannerList()
  },
  methods: {
    onGetBannerList() {
      http.post('/v1/banner', {})
        .then(response => {
          console.log(response)
          this.tableData = response.data
        })
        .catch(function (error) {
          console.log(error);
        })
    },
    CarouselUpdateSubmit(id) {
      http.post('/v1/getBannerInfo', { id: id })
        .then(response => {
          this.CarouselForm = response.data
          let fileList = [{ url: response.data.bannerUrl }]
          this.fileList = fileList
          console.log(this.CarouselForm)

          this.CarouselShow = true
        })
        .catch(function (error) {
          console.log(error);
        })
    },
    CarouseIsShowSubmit(id, isShow) {
      http.post('/v1/updateBannerShow', { id: id, isShow: isShow ? false : true })
        .then(response => {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.onGetBannerList()
        })
        .catch(function (error) {
          console.log(error);
        })
    },
    CarouselSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.onLoadInfo()
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
      this.CarouselForm.bannerUrl = ''
      this.fileList = []
    },
    imguploadSuccess(res) {
      console.log(res)
      this.CarouselForm.bannerUrl = res.data.info.url;
    },
    onLoadInfo() {
      http.post(this.CarouselForm.id ? '/v1/updateBanner' : '/v1/setBanner', this.CarouselForm)
        .then(response => {
          console.log(response)
          this.onGetBannerList()
          Object.keys(this.CarouselForm).forEach(key => {
            this.CarouselForm[key] = '';
          });
          this.CarouselShow = false
        })
        .catch(function (error) {
          console.log(error);
        })
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    }
  }
}
</script>

<style scoped>
.Carousel {
  width: 100%;
  min-height: 100%;
  padding: 15px;
  box-sizing: border-box;
}
.Carousel-Info {
  width: 100%;
  margin-bottom: 15px;
}
.Carousel-Info-li {
  width: 100%;
}
.Carousel-list {
  width: 100%;
  margin-bottom: 20px;
}
.Carousel-page-box {
  display: flex;
  justify-content: flex-end;
}
</style>